# coding:utf-8
"""
sql处理
"""
import MySQLdb as mdb
import ia_configure
#定义


#正式环境数据库和debug环境数据库
if ia_configure.is_debug == 0:
    database = 'qa_auto_time'
else:
    database = 'qa_auto_time_debug'
#关注接口查询mysql数据库
def follow_num(num):
    try:
        con = mdb.connect('10.13.5.157', 'root', 'qatest', 'qa')
        with con:
            cur = con.cursor()
            cur.execute("SELECT follow_num FROM %s WHERE id = 1"%database)
            result = cur.fetchone()
            if num == 1:
                res = int(result[0]) + 1
                cur.execute("UPDATE %s SET follow_num = '%s' where id = 1"%(database,res))
            return result[0]
    finally:
        con.close()

#数据库查询
def select_time(val):
    try:
        con = mdb.connect('10.13.5.157', 'root', 'qatest', 'qa')
        with con:
            cur = con.cursor()
            #执行一个查询
            cur.execute("SELECT %s from %s ORDER BY id DESC LIMIT 0,1"%(val,database))
            #取得上个查询的结果，是单个结果
            result = cur.fetchone()
            return result[0]
    finally:
        con.close()
#数据库更新数据
def update_time(model,value):
    try:
        con = mdb.connect('10.13.5.157', 'root', 'qatest', 'qa')
        with con:
            cur = con.cursor()
            cur.execute("UPDATE %s SET %s = '%s' where id = 1"%(database,model,value))
    finally:
        con.close()

